
hero.spell = {}
-- -- -- -- -- -- -- --
-- -- Fireball -- -- --
-- -- -- -- -- -- -- --
--fireballspr = Sprite.new ("spells/fireball.png",20,20,VRAM)
fireballspr = Image.load ("spells/onefireball.png",VRAM)

hero.fireball = {}

hero.fireball.wid = 20
hero.fireball.hig = 20

fbimax = 0
hero.fireballx = {}
hero.firebally = {}
hero.fireballdir = {}
hero.spell.fireball = 0
fbkill = 0

function hero.fireballNew() -- create a new fireball
	if 		hero.mana -hero.fireball.coast >= 0 
	then	hero.spell.fireball = 1
			fbimax = fbimax + 1
			hero.fireballx[fbimax] = hero.x + (hero.wid/2) - (hero.fireball.wid / 2)
			hero.firebally[fbimax] = hero.y + (hero.hig/2) - (hero.fireball.hig / 2)
			hero.fireballdir[fbimax] = hero.dir
			hero.mana = hero.mana - hero.fireball.coast
	else	hero.oht.new("Not enough mana!!!")
	end
end

function hero.fireballRun() -- move the existing fireballs
	for fbi = 1, fbimax do
		-- move the fireball
		if hero.fireballdir[fbi] == 5 then hero.firebally[fbi] = hero.firebally[fbi] + hero.fireball.speed -- 3
		elseif hero.fireballdir[fbi] == 3 then 	hero.fireballx[ fbi] = hero.fireballx[fbi] + hero.fireball.speed -- 2
		elseif hero.fireballdir[fbi] == 1 then 	hero.firebally[fbi] = hero.firebally[fbi] - hero.fireball.speed -- 1
		elseif hero.fireballdir[fbi] == 7 then 	hero.fireballx[fbi] = hero.fireballx[fbi] - hero.fireball.speed -- 4
		elseif hero.fireballdir[fbi] == 2 then 	hero.fireballx[fbi] = hero.fireballx[fbi] + hero.fireball.speed -- 5
		       hero.firebally[fbi] = hero.firebally[fbi] - hero.fireball.speed
		elseif hero.fireballdir[fbi] == 4 then 	hero.fireballx[fbi] = hero.fireballx[fbi] + hero.fireball.speed -- 6
		       hero.firebally[fbi] = hero.firebally[fbi] + hero.fireball.speed
		elseif hero.fireballdir[fbi] == 6 then 	hero.fireballx[fbi] = hero.fireballx[fbi] - hero.fireball.speed -- 7
		       hero.firebally[fbi] = hero.firebally[fbi] + hero.fireball.speed
		elseif hero.fireballdir[fbi] == 8 then 	hero.fireballx[fbi] = hero.fireballx[fbi] - hero.fireball.speed -- 8
		       hero.firebally[fbi] = hero.firebally[fbi] - hero.fireball.speed
		end
		
		-- test if fireball is out of screen
		if hero.fireballx[fbi] + hero.fireball.wid < 0 or hero.firebally[fbi] < 0 or hero.firebally[fbi] +  hero.fireball.hig > SCREEN_HEIGHT or hero.fireballx[fbi] > SCREEN_WIDTH then  fbkill = fbi	end
			
		-- test if fireball clash the enemy
		if enemy.max > 0 then
			for i = 1, enemy.max do
				if 	hero.fireballx[fbi] + hero.fireball.wid > enemy.x[i] and 
					hero.fireballx[fbi] < enemy.x[i] + enemy.wid[i] and 
					hero.firebally[fbi] + hero.fireball.hig > enemy.y[i]  and 
					hero.firebally[fbi] < enemy.y[i] + enemy.hig[i] 
				then 	enemy.damage(hero.fireball.damage,i)
						fbkill = fbi
						break
				end
			end
			--fireballspr:drawFrame(SCREEN_DOWN, hero.fireballx[fbi], hero.firebally[fbi], hero.fireballdir[fbi]-1)
      --TODO das mit den grad nochmal ueberdenken XD
      if hero.fireballdir[fbi] == 1 then shotdegree = 360 else shotdegree = ((hero.fireballdir[fbi] - 1) * 45) end
      Image.rotateDegree(fireballspr, shotdegree)
      screen.blit(SCREEN_DOWN, hero.fireballx[fbi], hero.firebally[fbi],fireballspr)
		end
	end
	
	if fbkill > 0 then  -- delete the fireball

		table.remove(hero.fireballx, fbkill)
		table.remove(hero.firebally, fbkill) 
		table.remove(hero.fireballdir, fbkill)
		fbimax = fbimax - 1
		fbkill = 0
	end

	if fbimax == 0 then hero.spell.fireball = 0 end
end
